package com.zw.pdm.module.est.service;

import com.zw.pdm.module.est.mapper.CardMapper;
import com.zw.pdm.module.est.model.WorkTimeQueryModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @Description:
 * @Author: China.sgl
 * @Date: 2019/12/6 08:42
 */
@Service
@SuppressWarnings("all")
public class WorkTimeQueryService {

    @Autowired
    private CardMapper cardMapper;

    /**
     * 列表
     */
    public List<WorkTimeQueryModel> findWithWorkTimeQuery(WorkTimeQueryModel workTimeQueryModel) {
        return cardMapper.findWithWorkTimeQuery(workTimeQueryModel);
    }

    public List<WorkTimeQueryModel> findWithWorkTimeTotalQuery(WorkTimeQueryModel workTimeQueryModel) {
        List<WorkTimeQueryModel> workTimeQueryModelList = cardMapper.findWithWorkTimeTotalQuery(workTimeQueryModel);
        List<WorkTimeQueryModel> workTimeQueryModelList1 = cardMapper.findWithWorkTimeTotalQuery1(workTimeQueryModel);
        for (int i = 0; i < workTimeQueryModelList.size(); i++) {
            for (int j = 0; j < workTimeQueryModelList1.size(); j++) {
                if (workTimeQueryModelList.get(i).getProCode().equals(workTimeQueryModelList1.get(j).getProCode()) &&
                        workTimeQueryModelList.get(i).getProName().equals(workTimeQueryModelList1.get(j).getProName()) &&
                        workTimeQueryModelList.get(i).getPart().equals(workTimeQueryModelList1.get(j).getPart()) &&
                        workTimeQueryModelList.get(i).getProcess().equals(workTimeQueryModelList1.get(j).getProcess())

                ) {
                    if ("".equals(workTimeQueryModelList.get(i).getEquName()) && "".equals(workTimeQueryModelList1.get(j).getEquName())) {
                        double dou = sumGS(workTimeQueryModelList.get(i).getTotal(), workTimeQueryModelList1.get(j).getWorkTimeOne(), workTimeQueryModelList1.get(j).getWorkTimeTwo());
                        workTimeQueryModelList.get(i).setTotal(dou);
                    } else if (workTimeQueryModelList.get(i).getEquName().equals(workTimeQueryModelList1.get(j).getEquName())) {
                        double dou = sumGS(workTimeQueryModelList.get(i).getTotal(), workTimeQueryModelList1.get(j).getWorkTimeOne(), workTimeQueryModelList1.get(j).getWorkTimeTwo());
                        workTimeQueryModelList.get(i).setTotal(dou);
                    }
                }
            }
        }
        return workTimeQueryModelList;
    }

    private double sumGS(double a, double b, double c) {
        double mm = aa(a, b);
        double reslut = aa(mm, c);

        return reslut;
    }

    public static Double aa(double a, double b) {
        if (a==0){
            return  b;
        }
        if (b==0){
            return  a;
        }
        int aa = (int) a * 100 / 100;
        int bb = (int) b * 100 / 100;
        int cc = (int) (Math.round(a * 100) % 100) + (int) (b * 100 % 100);
        return Double.parseDouble(aa + bb + cc / 60 + "." + (cc % 60 >=10? cc % 60:("0"+cc % 60)));

    }
}
